package org.nf.web.servlet.param;

import org.nf.web.servlet.MethodArgumentResolver;
import org.nf.web.servlet.ServletApi;
import org.nf.web.servlet.annotation.RequestAttribute;

import java.lang.reflect.Parameter;

/**
 * @author wangliang
 * @date 2024/6/14
 * 请求作用域参数解析器
 */
public class RequestAttributeMethodArgumentResolver implements MethodArgumentResolver {

    @Override
    public boolean supports(Parameter parameter) {
        return parameter.isAnnotationPresent(RequestAttribute.class);
    }

    @Override
    public Object resolve(Parameter parameter, ServletApi servletApi) throws Exception {
        String attributeName = parameter.getAnnotation(RequestAttribute.class).value();
        return servletApi.request().getAttribute(attributeName);
    }
}
